import mongoose from 'mongoose';

const orderSchema = mongoose.Schema({
    //订单用户
    user: {
        type: mongoose.Schema.Types.ObjectId,
        required: true,
        ref: 'User',
    },
    // 商品列表
    orderItems: [{
        //商品名称
        name: {
            type: String,
            required: true,
        },
        //商品数量
        qty: {
            type: Number,
            required: true,
        },
        //图片
        image: {
            type: String,
            required: true,
        },
        //价格
        price: {
            type: Number,
            required: true,
        },
        //商品信息
        product: {
            type: mongoose.Schema.Types.ObjectId,
            required: true,
            ref: 'Product',
        },
    }],
    // 快递地址
    shippingAddress: {
        //详细地址
        address: {
            type: String,
            required: true,
        },
        //省份
        province: {
            type: String,
            required: true,
        },
        //城市
        city: {
            type: String,
            required: true,
        },
        //邮编
        postalCode: {
            type: String,
            required: true,
        },
    },
    // 支付方式
    paymentMethod: {
        type: String,
        required: true,
    },
    // 支付结果
    paymentResult: {
        //结果的id
        id: {
            type: String,
        },
        //支付状态
        status: {
            type: String,
        },
        //支付更新时间
        update_time: {
            type: String,
        },
        //下单用户
        email_address: {
            type: String,
        },
    },
    //是否发货
    isDelivered: {
        type: Boolean,
        required: true,
        default:false,
    },
    //发货日期
    deliveredAt:{
        type: Date,
    },
    //快递费用
    shippingPrice:{
        type: Number,
        required: true,
        default:0,
    },
    //总价格
    totalPrice:{
        type: Number,
        required: true,
        default:0,
    },
    //是否支付
    isPaid:{
        type: Boolean,
        required: true,
        default:false,
    },
    //支付时间
    paidAt:{
        type: Date,
    }
}, {
    // 时间戳
    timestamps: true,
});

const Order = mongoose.model('Order', orderSchema);

export default Order;